const constant = require("../../util/constant.js");
const util = require('../../util/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        subtitle:'提交',
        index: 0,
        carnumber:'',
        zbrid:'',
        zbrbh:'',
        zbrname:'',
        zxbm:'',
        colors:['黑','白','红','橙','蓝','银','金','黄','绿','紫','棕','灰','其他'],
        colorindex:0,
        mycolor:'黑',
        types:['小型汽车','大型客车','小型货车','大型货车','摩托车','电动两轮','电动三轮','其他'],
        tpindex:0,
        mytype:'小型汽车',
        imgList: [],
        imgurls:[],
        imgpath:constant.host+"/content/jyjzimg/"+constant.cityid+"/",
        afaddress:'',
        PenaltyType:200,
        ajstat:0,
        isshowcx:0,
        ishowedit:1
    },
    platenumSuccess:function(res){
        //console.log("扫描结果")
        //console.log(res.detail.number.text);
        this.setData({
            carnumber:res.detail.number.text
        })
      },
      ColorPickerChange(e) {
        //console.log(e.detail.value);
        var idx=e.detail.value;
        var selcolor=this.data.colors[idx];
        
        //console.log(myxbr+'|'+myxbrbh);
        this.setData({
          mycolor:selcolor,
          colorindex:idx
        })
      },
      TPPickerChange(e) {
        //console.log(e.detail.value);
        var idx=e.detail.value;
        var seltypes=this.data.types[idx];
        
        //console.log(myxbr+'|'+myxbrbh);
        this.setData({
          mytype:seltypes,
          tpindex:idx
        })
      },
      computationTime (total) {
        total = total / 1000
       //计算整数天数s
       let day = parseInt(total / (24 * 60 * 60));
       //取得算出天数后剩余的秒数
       let afterDay = total - day * 24 * 60 * 60;
       //计算整数小时数
       let hour = parseInt(afterDay / (60 * 60));
       //取得算出小时数后剩余的秒数s
       let afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60;

       let min = parseInt(afterHour / 60)
       //console.log("分钟数："+min)
       if(min<6)
       {
         this.setData({
           isshowcx:1
         })
       }
       else
       {
        this.setData({
          ishowedit:0
        })
       }
      
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        var ajzbr=wx.getStorageSync('uname');
        var zbid=wx.getStorageSync('userid');
        var zbbh=wx.getStorageSync('uzfzbh');
        var bm=wx.getStorageSync('udept');
        var myid=0;
        if(option.id!=null && option.id!="")
        {
            myid=option.id;
        }
        this.setData({
            zbr:ajzbr,
            zbrbh:zbbh,
            zbrid:zbid,
            zbrname:ajzbr,
            ajid:myid,
            zxbm:bm
        })
        if(myid>0)
        {
        var that=this;
      wx.request({
          url: constant.host+'/appserv/getaycar.ashx',
          data: {
            aid:myid,
            k: constant.wkey
          },
          success: function (data) {
            var dat = data.data;
            if(dat.length>0)
            {
                var mydata=dat[0];
                var colors=that.data.colors;
                var cartypes=that.data.types;
                let carno=mydata.CarNumber;
                let carcolor=mydata.CarColor;
                let cartype=mydata.CarType;
                let afaddr=mydata.GetAddr;
                let penaltytype = mydata.PenaltyType;
                let mycolorindex=0;
                let typeindex=0;
                for(var i=0;i<colors.length;i++)
                {
                    if(carcolor==colors[i])
                    {
                        mycolorindex=i;
                    }
                }
                for(var i=0;i<cartypes.length;i++)
                {
                    if(cartype==cartypes[i])
                    {
                        typeindex=i;
                    }
                }
                let time1 = new Date(mydata.GetTime);
                let time2 = new Date();
                that.computationTime(time2 - time1);
                var ajimgs=[];
                if(mydata.AJImg!=null && mydata.AJImg.length>0)
                {
                    ajimgs=mydata.AJImg.split(',');
                }
                that.setData({
                    subtitle:'修改',
                    ajid:myid,
                    carnumber:carno,
                    colorindex:mycolorindex,
                    tpindex:typeindex,
                    afaddress:afaddr,
                    imgurls:ajimgs,
                    ajstat:mydata.NType,
                    PenaltyType:penaltytype
                })
            }
            
          }
        });
    }
    },
    cxview(){
      var myid=this.data.ajid;
      
      if(myid>0){
        wx.showModal({
          title: '撤销',
          content: '确定要撤销该处罚吗？',
          cancelText: '再看看',
          confirmText: '撤销',
          success: res => {
            if (res.confirm) {
                var that=this;
                wx.request({
                    url: constant.host+'/appserv/carstupdate.ashx',
                    data: {
                      k: constant.wkey,
                      cid:myid
                    },
                    method: 'GET',
                    header: {
                      'Accept': 'application/json'
                    },
                    success: function (res) {
                      //console.log(res.data)
                      var dat = res.data;
        
                      if (dat>0) {
                        wx.showToast({
                          title: '操作成功',
                        })
                        wx.navigateTo({
                          url: 'carslist',
                        });
                      }
                      else {
                        wx.showToast({
                          title: '操作失败',
                        })
                      }
                      wx.hideToast();
           
        
                    },
                  })
           
            }
          }
        })
      }
    },
    csaddr()
    {
      wx.chooseLocation({
        success:res=>{
            if(res.address!=''&&res.name!='')
            {
                this.setData({
                  afaddress:res.address
                })
            }
        }
      })
    },
    ChooseImage() {
        wx.chooseImage({
          count: 4, //默认9
          sizeType: ['compressed'],
          //sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], //从相册选择
          success: (res) => {
            
            if (this.data.imgList.length != 0) {
                var that=this;
                let userid=wx.getStorageSync('userid');
                wx.uploadFile({
                    url: constant.host+'/appserv/upimg.ashx',
                    formData: {
                      cid: constant.cityid,
                      k: constant.wkey,
                      uid:userid,
                      p:'/content/jyjzimg/'
                    },
                    filePath: res.tempFilePaths[0],
                    name: 'filename',
                    success: function (ress) {
                      //console.log(res.data)
                      var dat=ress.data; 
                      //console.log("a"+dat)
                      if (dat.length > 4) {
                    
                        var imgArr = that.data.imgurls;
                       
                        imgArr.push(dat);
                        //console.log(imgArr.join(','));
                        that.setData({
                            imgList: that.data.imgList.concat(res.tempFilePaths),
                            imgurls:imgArr
                        })           
                        
                      }
                      else {
                        wx.showToast({
                          title: '上传失败',
                        })
                      }           
                      wx.hideToast();              
                      
                    },
                    fail: function(res) {
                        wx.hideToast();
                        wx.showModal({
                            title: '错误提示',
                            content: '上传图片失败',
                            showCancel: false,
                            success: function(res) { }  
                        })
                  
                    }  
      
                  })
              //this.setData({
              //  imgList: this.data.imgList.concat(res.tempFilePaths)
              //})
            } else {
                var that=this;
                wx.uploadFile({
                    url: constant.host+'/appserv/upimg.ashx',
                    formData: {
                      cid: constant.cityid,
                      k: constant.wkey,
                      p:'/content/jyjzimg/'
                    },
                    filePath: res.tempFilePaths[0],
                    name: 'filename',
                    success: function (ress) {
                      //console.log(res.data)
                      var dat=ress.data; 
                      //console.log("b"+dat)
                      if (dat.length > 4) {
                    
                        var imgArr = that.data.imgurls;
                       
                        imgArr.push(dat);
                        //console.log(imgArr.join(','));
                        that.setData({
                            imgList: res.tempFilePaths,
                            imgurls:imgArr
                        })           
                        
                      }
                      else {
                        wx.showToast({
                          title: '上传失败',
                        })
                      }           
                      wx.hideToast();              
                      
                    },
                    fail: function(res) {
                        wx.hideToast();
                        wx.showModal({
                            title: '错误提示',
                            content: '上传图片失败',
                            showCancel: false,
                            success: function(res) { }  
                        })
                  
                    }  
      
                  })
              //this.setData({
              //  imgList: res.tempFilePaths
              //})
            }
          }
        });
      },
      ViewImage(e) {
          //console.log(e.currentTarget.dataset.url)
          var urls=this.data.imgurls;
          var imgpath=this.data.imgpath;
          var imgs=[];
          for(var i=0;i<urls.length;i++)
          {
              var myurl=imgpath+urls[i];
              imgs.push(myurl);
          }
    
        wx.previewImage({
          urls: imgs,
          current: imgpath+e.currentTarget.dataset.url
        });
      },
      DelImg(e) {
          //console.log(e.currentTarget.dataset.index);
          let delimg=this.data.imgurls[e.currentTarget.dataset.index];
          //console.log(delimg);
        wx.showModal({
          title: '删除',
          content: '确定要删除吗？',
          cancelText: '再看看',
          confirmText: '删除',
          success: res => {
            if (res.confirm) {
                var that=this;
                wx.request({
                    url: constant.host+'/appserv/upimgdel.ashx',
                    data: {
                      k: constant.wkey,
                      p: '/content/jyjzimg/'+constant.cityid+'/'+delimg
                    },
                    method: 'GET',
                    header: {
                      'Accept': 'application/json'
                    },
                    success: function (res) {
                      //console.log(res.data)
                      var dat = res.data;
        
                      if (dat==1) {
                        that.data.imgList.splice(e.currentTarget.dataset.index, 1);
                        that.data.imgurls.splice(e.currentTarget.dataset.index, 1);
                        that.setData({
                          imgList: that.data.imgList,
                          imgurls:that.data.imgurls
                        })
                      }
                      else {
                        wx.showToast({
                          title: '删除失败',
                        })
                      }
                      wx.hideToast();
           
        
                    },
                  })
           
            }
          }
        })
      },
      printview(){
        var aid=this.data.ajid;
        wx.navigateTo({
          url: 'carsview?aid='+aid,
        })
      },
      /** 时间戳格式化天数 **/
 
      handleSubmit: function (event) {
        var carnumber = event.detail.value.carnumber;
        var penaltytypes = event.detail.value.PenaltyType;
        if (carnumber == '') {
            util.showFailToast({
              title: '输入违停车牌号'
            });
      
            return;
          }
        var carcolor = this.data.mycolor;
        if (carcolor == '') {
            util.showFailToast({
              title: '请选择车辆颜色'
            });
      
            return;
          }
          var cartype=this.data.mytype;
          if (cartype == '') {
            util.showFailToast({
              title: '请选择车辆类型'
            });
      
            return;
          }
        var afdd=event.detail.value.afaddress;
        if (afdd == '') {
            util.showFailToast({
              title: '请输入违停地点'
            });
      
            return;
          }
          if(this.data.imgurls.length<3)
          {
            wx.showToast({
              title: '请上传至少三张违法照片',
              icon:'none'
            })
            return;
          }
        var zbid=this.data.zbrid;
        var zb=this.data.zbr;
        var zbbh=this.data.zbrbh;
 
          var aid=this.data.ajid;
       
          var bm=this.data.zxbm;
          var im=this.data.jzimg;
        
      // var formdata=event.detail.value
          var imgs=this.data.imgurls.join(',');
          //console.log(imgs);
        wx.request({
          url: constant.host+'/appserv/jzcreatcar.ashx',
     
          header:{
            //'Content-type':'application/x-www-form-urlencoded'
            'content-type':'application/x-www-form-urlencoded'
          },
          method: "POST",
          data:util.json2Form({
            id: aid,
            cno: carnumber,
            cclr: carcolor,
            ctp:cartype,
            afdd:afdd,
            zbid:zbid,
            zbr:zb,
            zbrbh:zbbh,
            dep:bm,
            img:imgs,
            penaltytype:penaltytypes,
            k:constant.wkey
            //formdata
          }), 
        
          success: function (res) {
              //console.log("r:"+res.data);
              let r=res.data;
              if(r>0)
              {
         
                wx.showToast({
                  title: "操作成功",
                  icon: 'success',
                  mask: true,
                  duration: 20000,
                  success: function (ress) {
                    wx.redirectTo({
                        url: 'weitingedit?id='+res.data,
                      })
                
                  }
    
                });
                
          
              }
              else
              {
                if(r==-10)
                {
                  wx.showToast({
                    title: '该车辆当天已被贴过罚单，请勿重复操作！',
                    icon:'none'
                  })
                }
                else
                {
                  util.showFailToast(
                    {
                      title: "操作失败，请重试"
                    }
                  )
                }
               
              }
            
          },
          fail:function(res)
          {
            console.log(res.data)
            util.showFailToast({
              title: "操作失败！请重试"
            });
          }
        });
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})